﻿
function addnewfuelsensorwin(){
    function msgUpdateSuccess() {
        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
        Ext.Msg.show({
            title: _TrackerMain_Title,
            msg: _NewForText_Success,
            buttons: Ext.Msg.OK,
            animEl: 'elId',
            icon: Ext.MessageBox.INFO
        });
    }
    
    function msgWarning(msgtext) {
        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
        Ext.Msg.show({
            title: _TrackerMain_Title,
            msg: msgtext,
            buttons: Ext.Msg.OK,
            animEl: 'elId',
            icon: Ext.MessageBox.WARNING
        });
    }
    
    function msgUpdateFailure() {
        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
        Ext.Msg.show({
            title: _TrackerMain_Title,
            msg: _NewForText_Failure,
            buttons: Ext.Msg.OK,
            animEl: 'elId',
            icon: Ext.MessageBox.WARNING
        });
    }


    var trackNameCombo = getTrackerCheckboxCombo(true);
    
    var cmbfuelsensor = new Ext.form.ComboBox({
        fieldLabel:_TrackerMain_FuelSensor,
        typeAhead: true,
        triggerAction: 'all',
        lazyRender:true,
        forceSelection :true,
        mode: 'local',
        store: new Ext.data.ArrayStore({
            id: 0,
            fields: [
                'sensorid',
                'sensorname'
            ],
            data: [[0,_TrackerMain_Null],[1, _TrackerMain_FuelC], [2, _TrackerMain_FuelR],[3,_TrackerMain_FuelSensorV1]]
        }),
        valueField: 'sensorid',
        displayField: 'sensorname'
   });
   
   cmbfuelsensor.setValue(0);
   
   var txttanksize = new Ext.form.NumberField({
        fieldLabel:_TrackerMain_TankSize,
        allowBlank: true,
        maxLength:3
   });
   
   var txtlowfuelpercent = new Ext.form.NumberField({
        fieldLabel:_TrackerMain_LowFuelPercent,
        allowBlank: false,
        blankText: _NewForText_CanNotEmpty,
        //emptyText:"0",
        maxLength:2,
        maxValue:99,
        minValue:0
   });
 
   var txthighfuelpercent = new Ext.form.NumberField({
        fieldLabel:_TrackerMain_HighFuelPercent,
        allowBlank: false,
        blankText: _NewForText_CanNotEmpty,
        //emptyText:"0",
        maxLength:2,
        maxValue:99,
        minValue:0
   });
   
    var addnewfuelsensorform = new Ext.form.FormPanel({
        bodyStyle: "padding-top:10px",
        width: 380,
        
        height: 180,
        defaultType: "textfield",
        items: [
            trackNameCombo,
            cmbfuelsensor,
            txtlowfuelpercent,
            txthighfuelpercent
        ],
        buttons: [{ text: _NewForText_Submit,
                handler: function() {
                    if (addnewfuelsensorform.getForm().isValid()) {
                        Ext.MessageBox.show({
                            msg: _NewForText_Saving,
                            progressText: _NewForText_Saving + '...',
                            width: 300,
                            wait: true,
                            waitConfig: { interval: 200 },
                            icon: 'download'
                        });
                        addnewfuelsensorform.form.submit({
                            url: "HandlerAddEditFuelSensor",
                            method: "POST",
                            params: { 
                                sssids:trackNameCombo.getValue(),
                                fuelsensorid:cmbfuelsensor.getValue(),
                                lowfuelpercent:txtlowfuelpercent.getValue(),
                                highfuelpercent:txthighfuelpercent.getValue()
                            },
                            success: function(form, action) {
                                //after success
                                var flag = action.result.success;
                                if (flag == true) {
                                    msgUpdateSuccess();
                                    addnewfuelsensorwin.hide();
                                }
                                else if (flag == "iserror") {
                                    msgWarning(_NewForText_AddNewDeviceFailedByExist);
                                }
                                
                            },
                            failure: function(form, action) {
                                msgUpdateFailure();
                            }
                        })
                    }
                }
            }]
        });

        
    var addnewfuelsensorwin = new Ext.Window({
        title: _TrackerMain_AddEditFuelSensor,
        height: 210,
        closeAction: 'hide',
        width: 400,
        plain: true,
        modal: 'true',
        buttonAlign: "center",
        constrain:true,
        items: [addnewfuelsensorform]
    });


    addnewfuelsensorwin.show();

}